import { useMutation } from "react-relay";
import {useCallback, useContext, useState} from "react";
import {
  ConnectionHandler,
  RecordSourceSelectorProxy,
  RecordProxy
} from "relay-runtime";
import { DeviceCommonInput } from "./__generated__/useCreateDeviceMutation.graphql";
import {UserContext} from "../routing/UserContext";

const graphql = require("babel-plugin-relay/macro");

//这个特别！返回{TodoEdge, User} 而不是直接返回TodoNode: Todo就可以了。!多出两个麻烦。
const mutation = graphql`
  mutation useRemoveDeviceMutation($id: ID!) {
    removeEQP(id:$id) 
  }
`;

export default function useRemoveDeviceMutation() {
  const [result, setResult] =useState(false);
  const [commit, isInFlight] = useMutation(mutation);
  return {
    call:useCallback(
      (id: string) => {
        return commit({
          variables: {
            id
          },
          updater: (store: RecordSourceSelectorProxy) => {
            //const payload = store.getRootField("removeEQP"); 非可以内省的基本类型输出，getRootField无法获得。
          },
          onCompleted: (response) => {
            //必须把上面的updater:配置函数删除掉才能执行这个onCompleted配置函数!!
            //上面输入response实际是 "raw" server response
            const { removeEQP } = response as { removeEQP: boolean };
            console.log("移除设备基本类型输出=", removeEQP);
          },
          //optimisticUpdater: (store: RecordSourceSelectorProxy) => {        }
        });
      },
      [commit]
    ),
    isInFlight,
    result
  };
}
